/*
 * PantallaJugador.java
 *
 * Created on 31 de agosto de 2008, 03:29 PM
 */
package GUI;

import Enlace.Trama;
import Enlace.TramaNombre;
import Enlace.Util;
import Enlace.mySingleton;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import Juego.*;
import java.util.Vector;
import javax.swing.JLabel;

/**
 *
 * @author  Propietario
 */
public class PantallaJugador extends javax.swing.JFrame {

    private Byte Pl;
    private int Cl;
    private Jugador JP;
    private int numerocartaPricipal;
    private Jugador JC = new Jugador(true);
    private List imagenes;
    private mySingleton ce;
    private Vector<String> tramas = new Vector();
    private String tramaCarta = new String();
    private String tramaEstado = new String();
    private String nombrePrincipal;
    private String nombreContrincante = new String();

    public PantallaJugador(List imagen, String port, Vector<String> namePlayer, int j1) throws Exception {
        imagenes = new ArrayList(imagen);
      /////////////////////////////////
        String aux = new String();
        Vector <String> vec = new Vector();
        aux = "1111111111111111111101101100100000000000001101010001001000011111111111111111111";
        vec.add(aux);
        System.out.println("estoy a punto de procesar");
        Procesar(vec);
      ////////////////////////////////
        Pl = 3;
        Cl = 3;
        ce = mySingleton.getInstance(port);
        TramaNombre tn = new TramaNombre();
        Trama t = new Trama();
        tramas = new Vector<String>(namePlayer);
        String carta1 = new String();
        String carta2 = new String();
        
        carta1 = t.AramarTrama("001101", 13, 6);
        carta2 = t.AramarTrama("010111", 23, 6);
        tramas.add(carta1);
        tramas.add(carta2);
        jLPeque1 = new JLabel();
        jLPeque2 = new JLabel();
        jLPeque3 = new JLabel();
        jLPeque4 = new JLabel();
        jLPeque5 = new JLabel();
        jLReves = new JLabel();
        System.out.println(tramas);
        Procesar(tramas);
        
        nombrePrincipal = new String(tn.capturarNombre(tramas));
        initComponents();
        JP = new Jugador(true);
        JP = new Jugador(false);
        
       // JC = new Jugador(true);
        //JC = new Jugador(false);
        
        MostradorTrama.setVisible(true);
        BTNEnviar.setEnabled(false);
        BTNEEstado.setEnabled(false);
        jBCarta.setVisible(false);
        jBParar.setVisible(false);
        jLCarta1.setIcon((ImageIcon) imagenes.get(JP.pedirCarta()));
        jLCarta2.setIcon((ImageIcon) imagenes.get(JP.pedirCarta()));
        jLReves.setIcon((ImageIcon) imagenes.get(53));
        
        
        this.setTitle("Juego");

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jBCarta = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jBParar = new javax.swing.JButton();
        jLCarta1 = new javax.swing.JLabel();
        jLCarta2 = new javax.swing.JLabel();
        jLUsuario = new javax.swing.JLabel();
        jLContrincante = new javax.swing.JLabel();
        jLPeque1 = new javax.swing.JLabel();
        jLReves = new javax.swing.JLabel();
        jLCarta3 = new javax.swing.JLabel();
        jLCarta4 = new javax.swing.JLabel();
        jLCarta5 = new javax.swing.JLabel();
        jLCarta6 = new javax.swing.JLabel();
        jLPeque2 = new javax.swing.JLabel();
        jLPeque3 = new javax.swing.JLabel();
        jLPeque4 = new javax.swing.JLabel();
        jLPeque5 = new javax.swing.JLabel();
        MostradorTrama = new javax.swing.JTextField();
        BTNEnviar = new javax.swing.JButton();
        BTNEEstado = new javax.swing.JButton();
        BTNlisto = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        jBCarta.setBackground(new java.awt.Color(0, 0, 0));
        jBCarta.setFont(new java.awt.Font("Arial", 1, 11));
        jBCarta.setForeground(new java.awt.Color(204, 102, 0));
        jBCarta.setText("Carta");
        jBCarta.setBorder(null);
        jBCarta.setFocusable(false);
        jBCarta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCartaActionPerformed(evt);
            }
        });

        jSeparator1.setBackground(new java.awt.Color(204, 102, 0));
        jSeparator1.setForeground(new java.awt.Color(204, 102, 0));

        jBParar.setBackground(new java.awt.Color(0, 0, 0));
        jBParar.setFont(new java.awt.Font("Arial", 1, 11));
        jBParar.setForeground(new java.awt.Color(204, 102, 0));
        jBParar.setText("Plantar");
        jBParar.setFocusable(false);
        jBParar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPararActionPerformed(evt);
            }
        });

        jLUsuario.setFont(new java.awt.Font("Arial", 1, 14));
        jLUsuario.setForeground(new java.awt.Color(204, 102, 0));
        jLUsuario.setText(nombrePrincipal);

        jLContrincante.setFont(new java.awt.Font("Arial", 1, 14));
        jLContrincante.setForeground(new java.awt.Color(204, 102, 0));
        jLContrincante.setText(nombreContrincante);

        jLPeque1.setIcon((ImageIcon)imagenes.get(numerocartaPricipal));

        MostradorTrama.setText("ok");
        MostradorTrama.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MostradorTramaActionPerformed(evt);
            }
        });

        BTNEnviar.setBackground(new java.awt.Color(0, 0, 0));
        BTNEnviar.setFont(new java.awt.Font("Arial", 1, 11));
        BTNEnviar.setForeground(new java.awt.Color(204, 102, 0));
        BTNEnviar.setText("Enviar");
        BTNEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTNEnviarActionPerformed(evt);
            }
        });

        BTNEEstado.setBackground(new java.awt.Color(0, 0, 0));
        BTNEEstado.setFont(new java.awt.Font("Arial", 1, 11));
        BTNEEstado.setForeground(new java.awt.Color(204, 102, 0));
        BTNEEstado.setText("E Estado");
        BTNEEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTNEEstadoActionPerformed(evt);
            }
        });

        BTNlisto.setBackground(new java.awt.Color(0, 0, 0));
        BTNlisto.setFont(new java.awt.Font("Arial", 1, 11));
        BTNlisto.setForeground(new java.awt.Color(204, 102, 0));
        BTNlisto.setText("Listo");
        BTNlisto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTNlistoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLCarta1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLCarta2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLCarta3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLCarta4, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLCarta5, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLCarta6, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(184, 184, 184)
                        .addComponent(jBCarta, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBParar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(BTNEnviar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(BTNEEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(BTNlisto))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(MostradorTrama, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 763, Short.MAX_VALUE)
                            .addComponent(jLUsuario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLContrincante, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jLPeque1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLReves, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLPeque2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLPeque4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLPeque3, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLPeque5, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLCarta5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(jLCarta4, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(jLCarta3, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(jLCarta1, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(jLCarta2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(jLCarta6, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBCarta, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBParar)
                    .addComponent(BTNEnviar)
                    .addComponent(BTNEEstado)
                    .addComponent(BTNlisto))
                .addGap(11, 11, 11)
                .addComponent(MostradorTrama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLContrincante, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLPeque1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPeque5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPeque4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPeque2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLReves, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(jLPeque3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                .addContainerGap(108, Short.MAX_VALUE))
        );

        jMenu1.setText("Archivo");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Acerca de...");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jBCartaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCartaActionPerformed
// TODO add your handling code here:
            // TODO add your handling code here:
            Byte b = JP.pedirCarta();
            switch (Pl) {
                case 3:
                    jLCarta3.setIcon((ImageIcon) imagenes.get(b));
                    break;
                case 4:
                    jLCarta4.setIcon((ImageIcon) imagenes.get(b));
                    break;
                case 5:
                    jLCarta5.setIcon((ImageIcon) imagenes.get(b));
                    break;
                case 6:
                    jLCarta6.setIcon((ImageIcon) imagenes.get(b));
                    break;
            }
            if (JP.sePaso()) {
                jBCarta.setEnabled(false);
            }
            Pl++;
            System.out.println("pidiendo carta");

            Trama trama = new Trama();
            String tramaaux = new String();
            String datosTrama = new String();
            datosTrama = Integer.toBinaryString((int) b);
            tramaCarta = trama.AramarTrama(datosTrama, (int) b, 6);
            ce.sendTrama(tramas); //CAMPO PARA ENVIAR LA TRAMA
            tramas.clear();
            while (true) {
                if (mySingleton.getInstance("COM1").isReady(tramas)){ //--No se puede quemar el puerto
                    System.out.println("OK!!!!");
                    ce.getBuffer(tramas);
                    Procesar(tramas);
                    //tramas.clear();
                    break;
                }
            }
            MostradorTrama.setVisible(true);
            MostradorTrama.setText(tramaCarta);
            jBCarta.setEnabled(false);
            jBParar.setEnabled(false);
            BTNEnviar.setEnabled(true);
                
    }//GEN-LAST:event_jBCartaActionPerformed

    private void jBPararActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPararActionPerformed
        // TODO add your handling code here:
        JP.planto();
        Trama trama = new Trama();
        String tramaaux = new String();
        tramaaux = trama.AramarTrama("110111", 55, 7);
        MostradorTrama.setVisible(true);
        MostradorTrama.setText(tramaaux);
        /*
         * ce.sendTrama(tramas); //CAMPO PARA ENVIAR LA TRAMA
         */
        BTNEnviar.setEnabled(false);
        jBParar.setEnabled(false);
        jBCarta.setEnabled(false);
        BTNEEstado.setEnabled(true);

    }//GEN-LAST:event_jBPararActionPerformed

    private void BTNEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTNEnviarActionPerformed
        // TODO add your handling code here:
        tramaCarta = "";
        tramaEstado = "";
        boolean sepaso = false;
        boolean tiene21 = false;
        boolean planto = false;

        Trama trama = new Trama();
        String tramaaux = new String();

        String aux = new String();
        tramaCarta = MostradorTrama.getText();

        planto = JP.estaPlantado();
        sepaso = JP.sePaso();
        tiene21 = JP.tiene21();

        if (tiene21) {
            tramaEstado = trama.AramarTrama("110110", 54, 7);
        } else if (planto) {
            tramaEstado = trama.AramarTrama("110111", 55, 7);

        } else if (sepaso) {
            tramaEstado = trama.AramarTrama("111000", 56, 7);
        } else if (tiene21 && planto) {
            tramaEstado = trama.AramarTrama("111001", 57, 7);
        } else {
            jBCarta.setEnabled(true);
            jBParar.setEnabled(true);
        }
        tramas.add(tramaCarta);
        tramas.add(tramaEstado);

        //*********************************ENVIO DE TRAMAS
        ce.sendTrama(tramas); //CAMPO PARA ENVIAR LA TRAMA
        tramas.clear();
        while (true) {
            if (mySingleton.getInstance("COM1").isReady(tramas)) { //--No se puede quemar el puerto
                System.out.println("OK!!!!");
                ce.getBuffer(tramas);
                Procesar(tramas);
                //tramas.clear();
                break;
            }
        }
        //*********************************ENVIO DE TRAMAS

        BTNEnviar.setEnabled(false);
        
        
        
}//GEN-LAST:event_BTNEnviarActionPerformed

    private void BTNEEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTNEEstadoActionPerformed
        // TODO add your handling code here:
        tramaEstado = "";
        boolean sepaso = false;
        boolean tiene21 = false;
        boolean planto = false;

        Trama trama = new Trama();

        if (tiene21) {
            tramaEstado = trama.AramarTrama("110110", 54, 7);
        } else if (planto) {
            tramaEstado = trama.AramarTrama("110111", 55, 7);
        } else if (sepaso) {
            tramaEstado = trama.AramarTrama("111000", 56, 7);
        } else if (tiene21 && planto) {
            tramaEstado = trama.AramarTrama("111001", 57, 7);
        }

        tramas.add(tramaEstado);

        //*********************************ENVIO DE TRAMAS
        ce.sendTrama(tramas); //CAMPO PARA ENVIAR LA TRAMA
        tramas.clear();
        while (true) {
            if (mySingleton.getInstance("COM1").isReady(tramas)) { //--No se puede quemar el puerto
                System.out.println("OK!!!!");
                ce.getBuffer(tramas);
                Procesar(tramas);
                //tramas.clear();
                break;
            }
        }
        //*********************************ENVIO DE TRAMAS

        BTNEnviar.setEnabled(false);
        jBParar.setEnabled(false);
        jBCarta.setEnabled(false);
        BTNEEstado.setEnabled(false);
                
        
    }//GEN-LAST:event_BTNEEstadoActionPerformed

//----------metodo para procesar las tramas - (esta sin terminar)---------------
    public void Procesar(Vector<String> tramas) {
        String bControl = new String();
        String cartaAux = new String();

        String estadoAux = new String();

        String tallaAux = new String();

        TramaNombre tn = new TramaNombre();

        int numeroCarta = 0, est = 0;
        char auxNumero;
        char auxEs;
        Vector<String> nombre = new Vector();
        Util u =  Util.getInstance();

        for (int i = 0; i < tramas.size(); i++) {
            String bConTrama = tramas.get(i);
            if (u.tipoTrama(bConTrama) == 1) // si la trama es de nombre
            {
                nombre.add(bConTrama);                
                }
            else if (u.tipoTrama(bConTrama) == 3) // si la trama es de carta
            {
                cartaAux = bConTrama.substring(24, 30);
                auxNumero = (char) Integer.parseInt(cartaAux, 2);
                numeroCarta = (int) auxNumero;
                numerocartaPricipal = numeroCarta;
                System.out.println("Este es el NuXXXXXXXXXXXXXXXXXXXX: "+numeroCarta);
                JC.recibirCarta(numeroCarta);

                //ImageIcon i53 = new ImageIcon("image/Back.gif");
                switch (Cl) {
                    case 2:
                        jLPeque2.setIcon((ImageIcon) imagenes.get(numeroCarta));                   
                        break;
                    case 3:
                        jLPeque3.setIcon((ImageIcon)imagenes.get(numeroCarta));
                        break;
                    case 4:
                        jLPeque4.setIcon((ImageIcon) imagenes.get(numeroCarta));
                        break;
                    case 5:
                        jLPeque5.setIcon((ImageIcon) imagenes.get(numeroCarta));
                }
                Cl++;
            } else if (u.tipoTrama(bConTrama) == 4) // si la trama es de estado
            {
                estadoAux = bConTrama.substring(24, 30);
                auxEs = (char) Integer.parseInt(estadoAux, 2);
                est = (int) auxEs;
                if (est == 54) {
                    JC.set21(true);
                } else if (est == 55) {
                    JC.planto();
                } else if (est == 56) {
                    JC.setSePaso(true);
                } else if (est == 57) {
                    JC.set21(true);
                    JC.planto();
                }
            } else if (u.tipoTrama(bConTrama) == 7) // si la trama es de seleccion de dealer
            {
                int cualEs = 1;
                tallaAux = bConTrama.substring(24, 42);
                for (int k = 0; k < tallaAux.length(); k++) {
                    if (tallaAux.charAt(k) == 0) {
                        cualEs = 0;
                    }
                }
                JP.setDealer(cualEs);
                if (cualEs == 1) {
                    JC.setDealer(0);
                } else {
                    JC.setDealer(1);
                }
            }
        }
        //-----se pone el nombre del 2 jugador en el label
        nombreContrincante = tn.capturarNombre(nombre);
    //jLContrincante.setText(nombreContrincante);
    }

    private void MostradorTramaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MostradorTramaActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_MostradorTramaActionPerformed

    private void BTNlistoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTNlistoActionPerformed

            BTNEnviar.setEnabled(false);
            BTNEEstado.setEnabled(false);
            BTNlisto.setVisible(false);
            System.out.println("---");
            System.out.println("tramas a enviar : "+tramas);
            ce.sendTrama(tramas);
            tramas.clear();
            System.out.println("AFURA DE MI WHILE");
            while (true) {
                if (mySingleton.getInstance("COM1").isReady(tramas)){ //no se puede quemar el puerto
                    System.out.println("OK!!!!");
                    ce.getBuffer(tramas);
                    Procesar(tramas);
                    break;
                }
            }
            jBCarta.setVisible(true);
            jBParar.setVisible(true);
    }//GEN-LAST:event_BTNlistoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BTNEEstado;
    private javax.swing.JButton BTNEnviar;
    private javax.swing.JButton BTNlisto;
    private javax.swing.JTextField MostradorTrama;
    private javax.swing.JButton jBCarta;
    private javax.swing.JButton jBParar;
    private javax.swing.JLabel jLCarta1;
    private javax.swing.JLabel jLCarta2;
    private javax.swing.JLabel jLCarta3;
    private javax.swing.JLabel jLCarta4;
    private javax.swing.JLabel jLCarta5;
    private javax.swing.JLabel jLCarta6;
    private javax.swing.JLabel jLContrincante;
    private javax.swing.JLabel jLPeque1;
    private javax.swing.JLabel jLPeque2;
    private javax.swing.JLabel jLPeque3;
    private javax.swing.JLabel jLPeque4;
    private javax.swing.JLabel jLPeque5;
    private javax.swing.JLabel jLReves;
    private javax.swing.JLabel jLUsuario;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
    // cerrar ventana
    @Override
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
        }
    }
}
